//
//  SPPettingListController.swift
//  SparklingPets
//
//  Created by iOS-Czz on 2023/10/26.
//

import UIKit
import WMZDialog

class SPPettingListController: SPBaseController,UITextFieldDelegate {
    lazy var viewModel: SPPetViewModel = {
        return SPPetViewModel()
    }()
    @IBOutlet var typeIcon: UIImageView!
    @IBOutlet var typeLabel: UILabel!
    var selectType = 6
    lazy var ideaView: SPPetingFilterView = {
        let view = Bundle.main.loadNibNamed("SPPetingFilterView", owner: self, options: nil)?.first as! SPPetingFilterView
        view.frame = keyWindow!.frame
        view.onSave = { index in
            self.selectType = index
            if index == 6 {
                self.typeIcon.isHidden = true
                self.typeLabel.text = "All"
            }else{
                self.typeIcon.isHidden = false
                self.typeLabel.text = PET_NAMES[index]
            }
            self.getPageData()
        }
        return view
    }()
  
    @IBOutlet var pageTab: UITableView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getPageData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.wx_navigationBar.isHidden = true
        setTab()
        
        // Do any additional setup after loading the view.
    }
   
    
    func getPageData(){
        self.viewModel.loadData(self.selectType, onSuccess: { models in
            self.pageTab.reloadData()
        })
    }
    func setTab(){
        pageTab.dataSource = self
        pageTab.delegate = self
        pageTab.register(UINib(nibName: "SPPetingListCell", bundle: .main), forCellReuseIdentifier: "SPPetingListCell")
        
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        getPageData()
        self.view.endEditing(true)
        return true
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
        getPageData()
        self.view.endEditing(true)
    }
    @IBAction func clickAddSomthingsAction(_ sender: Any) {
        self.navigationController?.pushViewController(SPPettingAddController(), animated: true)
    }
    @IBAction func clickTypeAction(_ sender: Any) {
        self.ideaView.selectIndex = 6
       keyWindow!.addSubview(self.ideaView)
    }
    
}

extension SPPettingListController:UITableViewDataSource,UITableViewDelegate{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.viewModel.dataSource.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:SPPetingListCell = tableView.dequeueReusableCell(withIdentifier: "SPPetingListCell")  as! SPPetingListCell
        cell.model = self.viewModel.dataSource[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    }
    func addSub(){
        let label = UILabel()
        let attrString = NSMutableAttributedString(string: "")
        label.frame = CGRect(x: 205, y: 431.5, width: 52, height: 13)
        label.numberOfLines = 0
        let attr: [NSAttributedString.Key : Any] = [.font: UIFont(name: "ssa", size: 13) as Any,.foregroundColor: UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)]
        attrString.addAttributes(attr, range: NSRange(location: 0, length: attrString.length))
        label.attributedText = attrString
        view.addSubview(label)
    }
}
